package com.cloud.common.support;

import com.cloud.common.context.AppContext;
import com.cloud.common.sentinel.Sentinel;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.internet.MimeMessage;

/**
 * 邮件发送者
 *
 * @author fengwenjin
 */
@Sentinel
@Component
public class EmailSender {

    /**
     * 发送邮件
     *
     * @param from
     * @param to
     * @param subject
     * @param content
     * @throws Exception
     */
    public void send(String from, String to, String subject, String content) throws Exception {
        JavaMailSender mailSender = AppContext.getBean(JavaMailSender.class);
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(from);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(content, true);
        mailSender.send(message);
    }
}
